<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="referrer" content="no-referrer">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
    <style>
        .logo {
            width: 66px;
            height: 100px;
        }

        .number {
            width: 50px;
        }
    </style>
</head>

<body>
    <div id="app" class="container">

        <table class="table table-hover">
            <thead>
                <tr>
                    <th>全选<input type="checkbox" @change="handleAllChecked" v-model="allChecked"></th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item of carts">
                    <td><input type="checkbox" @change="handleChecked" v-model="item.isSelected"></td>
                    <td><img :src="item.productCover" class="logo"></td>
                    <td>{{item.productPrice}}</td>
                    <td><input class="number" type="number" v-model.number="item.productCount"></td>
                    <td>{{item.productPrice * item.productCount | handleSum}}</td>
                    <td><button class="btn btn-danger" :data-aid="item.id" @click="handleDelete">删除</button></td>
                </tr>
            </tbody>
        </table>
        <p>总计:{{count()}}</p>

    </div>
    <script>
        new Vue({
            data() {
                return {
                    carts: [],
                    allChecked: ""
                }
            },
            methods: {
                handleAllChecked() {
                    this.carts.forEach(item => {
                        item.isSelected = this.allChecked
                    })
                },
                handleChecked() {
                    this.allChecked = this.carts.every(item => item.isSelected)
                },
                count() {
                    var count = 0;
                    this.carts.forEach(item => {
                        if (item.isSelected) {
                            count += item.productPrice * item.productCount;
                        }
                    })
                    return Math.round(count * 100) / 100
                },
                handleDelete(e) {
                    var { aid } = e.target.dataset;
                    var index = this.carts.findIndex(item => item.id == aid);
                    this.carts.splice(index, 1)
                }
            },
            mounted() {
                var url = "http://47.108.197.28:8000/cart";
                $.ajax({
                    url,
                    success: res => {
                        this.carts = res
                        this.allChecked = res.every(item => item.isSelected)
                    }
                })
            },
            filters: {
                handleSum(val) {
                    return "￥" + val.toFixed(2)
                }
            }
        }).$mount("#app")
    </script>
</body>

</html>